package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     *
     * @param session
     * @param employee
     * @return
     */
    @PostMapping("login")
    public R<Employee> login(HttpSession session, @RequestBody Employee employee) {
        //将页面提交的密码进行md5加密
        String password = employee.getPassword();//获取明文密码
        password = DigestUtils.md5DigestAsHex(password.getBytes());//密码加密

        //根据页面提交的用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        //如果没有查询到则返回登录失败的结果
        if (emp == null) {
            return R.error("用户名或密码错误");
        }

        //密码比对 如果不一致则返回失败结果
        if (!emp.getPassword().equals(password)) {
            return R.error("用户名或密码错误");
        }

        //查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0) {
            return R.error("账号已被禁用");
        }

        //登录成功，将员工id存入Session并返回登录成功结果
        session.setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出
     *
     * @param request
     * @return
     */
    @PostMapping("logout")
    public R<String> Logout(HttpServletRequest request) {
        //清理Session中保存的当前登录员工的id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 增加员工
     *
     * @param request
     * @param employee
     * @return
     */

    @PostMapping
    private R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        //初始密码为123456 进行md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employee.setCreateTime(LocalDateTime.now());//创建时间
      /*  employee.setUpdateTime(LocalDateTime.now());//修改时间

        //获取当前登录的用户id
        Long emp = (Long) request.getSession().getAttribute("employee");

        employee.setCreateUser(emp);
        employee.setUpdateUser(emp);
*/
        //添加新用户
        employeeService.save(employee);

        return R.success("新增员工成功");
    }

    /**
     * 分页
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //构造分页构造器
        Page<Employee> pageInfo = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 修改状态
     *
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    private R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        //获取登录用户id
        /*Long emp = (Long) request.getSession().getAttribute("employee");
        //更新修改人
        employee.setUpdateUser(emp);
        //更新修改时间
        employee.setUpdateTime(LocalDateTime.now());*/
        //根据id修改
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        if(employee!=null){
            return R.success(employee);
        }
        return R.error("没有查找到员工信息");
    }
}
